﻿$(function () {
    GetAllSpeakers();
    $('.ShDisc').click(function () {
        if ($(this).parent('.speakerData').children('.descrDiv').is(':hidden')) {
            $(this).parent('.speakerData').children('.descrDiv').show("slide", { direction: "left" }, 1000);
            $(this).parent('.speakerData').children('.descrDiv').css('visibility', 'visible');
            $(this).html('Hide Details');
        }
        else {
            $(this).parent('.speakerData').children('.descrDiv').hide("slide", { direction: "left" }, 1000);
            $(this).parent('.speakerData').children('.descrDiv').css('visibility', 'hidden');
            $(this).html('Show Details');
        }

    });
});

function GetAllSpeakers() {
    $('#SpeakersTemp').empty();
    ajaxCall('../DataService.asmx/GetAllSpeakers', false, "",
            function (msg) {
                if (msg.d == null) {
                    $('#SpeakersTemp').html("No speakers found in the database");
                } 
                else {
                    for (i in msg.d) {
                        var speakers = [
                            { ID: msg.d[i].ID, FirstName: msg.d[i].FirstName, LastName: msg.d[i].LastName, CompanyName: msg.d[i].CompanyName, Country: msg.d[i].Country, ExpertIn: msg.d[i].ExpertIN, Link: msg.d[i].Link, Description: msg.d[i].Description },
                        ];

                        $('#SpeakerTemplate').tmpl(speakers).appendTo('#SpeakersTemp');
                    }
                }
            },
            function () {
                alert("Error happend while reading data from database");
            }
        );
}

function DeleteSpeaker(id) {
    if (confirm("Are you sure that you want to delete this item?")) {
        var data = '{"ID":"' + id + '"}';
        ajaxCall('../DataService.asmx/DeleteSpeaker', false, data,
                function () {
                    alert("Deleting process was successful!");
                    GetAllSpeakers();
                },
                function () {
                    alert("Error happend while deleting this item");
                }
            );
     }
}


    